package com.alinesno.cloud.initializr.starter.bean;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.alinesno.cloud.initializr.starter.bean.templates.DependencyBean;
import com.alinesno.cloud.initializr.starter.bean.templates.DependencyEnums;
import com.alinesno.cloud.initializr.starter.bean.templates.MavenTemplateRootBean;
import com.alinesno.cloud.initializr.starter.bean.templates.ProjectBean;

/**
 * 依赖工具对象
 * 
 * @author WeiXiaoJin
 * @since 2021年8月2日 下午5:39:15
 */
public class DependencyUtils {

	private static final Logger log = LoggerFactory.getLogger(DependencyUtils.class) ; 
	
	/**
	 * 获取到底目录
	 * 
	 * @param jsonStr
	 * @return
	 */
	public static MavenTemplateRootBean root(String jsonStr) {

		MavenTemplateRootBean root = new MavenTemplateRootBean();

		JSONObject json = JSONObject.parseObject(jsonStr);
		JSONObject projectJson = json.getJSONObject("project");
		ProjectBean projectBean = JSONObject.toJavaObject(projectJson, ProjectBean.class);
		
		root.setProject(projectBean);
		log.debug("projectBean = {}" , projectBean);
		
		DependencyBean api = depen(json, DependencyEnums.API) ; 
		DependencyBean starter = depen(json, DependencyEnums.API) ; 
		DependencyBean gateway = depen(json, DependencyEnums.API) ; 
		DependencyBean web = depen(json, DependencyEnums.API) ; 
		DependencyBean boot = depen(json, DependencyEnums.API) ; 

		Map<String , DependencyBean> map =  new HashMap<String , DependencyBean>() ; 
		
		map.put(DependencyEnums.API.label(), api); 
		map.put(DependencyEnums.STARTER.label(), starter); 
		map.put(DependencyEnums.GATEWAY.label(), gateway); 
		map.put(DependencyEnums.WEB.label(), web); 
		map.put(DependencyEnums.BOOT.label(), boot); 
		
		root.setDependencies(map);
		
		return root;
	}

	/**
	 * 調整配置
	 * 
	 * @param dep
	 * @return
	 */
	public static DependencyBean depen(JSONObject json, DependencyEnums dep) {
		JSONObject dependeciesJson = json.getJSONObject("dependencies").getJSONObject(dep.label());
		DependencyBean d = JSONObject.toJavaObject(dependeciesJson, DependencyBean.class);
		return d;
	}

	public static void main(String[] args) {
		
		String jsonStr = "{\r\n" + 
				"  \"project\": {\r\n" + 
				"    \"name\": \"project-demo-deply\",\r\n" + 
				"    \"type\": \"maven\",\r\n" + 
				"    \"package-scan\": \"com.alinesno.cloud.data\"\r\n" + 
				"  },\r\n" + 
				"  \"dependencies\": {\r\n" + 
				"    \"api\": {\r\n" + 
				"      \"group-id\": \"com.alinesno.cloud.data\",\r\n" + 
				"      \"artifact-id\": \"alinesno-cloud-adapter-api\",\r\n" + 
				"      \"version\": \"1.2.2\"\r\n" + 
				"    },\r\n" + 
				"    \"boot\": {\r\n" + 
				"      \"group-id\": \"com.alinesno.cloud.data\",\r\n" + 
				"      \"artifact-id\": \"alinesno-cloud-adapter-boot\",\r\n" + 
				"      \"version\": \"1.2.2\"\r\n" + 
				"    },\r\n" + 
				"    \"gateway\": {\r\n" + 
				"      \"group-id\": \"com.alinesno.cloud.data\",\r\n" + 
				"      \"artifact-id\": \"alinesno-cloud-adapter-gateway\",\r\n" + 
				"      \"version\": \"1.2.2\"\r\n" + 
				"    },\r\n" + 
				"    \"starter\": {\r\n" + 
				"      \"group-id\": \"com.alinesno.cloud.data\",\r\n" + 
				"      \"artifact-id\": \"alinesno-cloud-adapter-starter\",\r\n" + 
				"      \"version\": \"1.2.2\"\r\n" + 
				"    },\r\n" + 
				"    \"web\": {\r\n" + 
				"      \"group-id\": \"com.alinesno.cloud.data\",\r\n" + 
				"      \"artifact-id\": \"alinesno-cloud-adapter-web\",\r\n" + 
				"      \"version\": \"1.2.2\"\r\n" + 
				"    }\r\n" + 
				"  }\r\n" + 
				"}" ; 
		
		MavenTemplateRootBean root = DependencyUtils.root(jsonStr) ; 
		System.out.println("dependencyList = " + JSONObject.toJSONString(root));
		
	}
	
}
